package com.i18n.moka;

import java.time.LocalDateTime;
import java.util.*;
import java.util.regex.*;

public class DateParser {

    static   Map<Integer, String> calendarIdentifierMap = new HashMap<>();
    static List<Integer> list = new ArrayList<>();
    static {

        list.addAll(Arrays.asList(1,13,7));
//        list.addAll(Arrays.asList(1));
        calendarIdentifierMap.put(Calendar.YEAR, "YEAR");
        calendarIdentifierMap.put(Calendar.MONTH, "MONTH");
        calendarIdentifierMap.put(Calendar.DAY_OF_MONTH, "DAY_OF_MONTH");
        calendarIdentifierMap.put(Calendar.HOUR, "HOUR");
        calendarIdentifierMap.put(Calendar.MINUTE, "MINUTE");
        calendarIdentifierMap.put(Calendar.SECOND, "SECOND");
        calendarIdentifierMap.put(Calendar.DAY_OF_WEEK, "DAY_OF_WEEK");
    }

    static String locale = "en-US";

    public static void main(String[] args) {

        String input = "{(YEAR年)(MONTH月)(DAY_OF_MONTH日)} {(HOUR点)(MINUTE分)(SECOND秒)} {(DAY_OF_WEEK.displayName)}";
        String input2 ="{(MONTH.displayName) (DAY_OF_MONTH), (YEAR)} {(HOUR):(MINUTE):(SECOND)} {(DAY_OF_WEEK.displayName)}";
        String input3 = "{(DAY_OF_MONTH tháng) (MONTH năm) (YEAR)} {(HOUR):(MINUTE):(SECOND)} {(DAY_OF_WEEK.displayName)}";
        String input4 = "{(YEAR)-(MONTH)-(DAY_OF_MONTH)} {(HOUR):(MINUTE):(SECOND)} {(DAY_OF_WEEK)}";
        // 正则表达式匹配花括号中的整个内容
        Pattern pattern = Pattern.compile("\\{([^}]*)\\}");
        Matcher matcher = pattern.matcher(input4);


        List<String> result = new ArrayList<>();

        // 提取花括号中的内容
        while (matcher.find()) {
            String content = matcher.group(1).trim();  // 获取花括号内的内容
            // 循环处理括号内的内容
            content = processBracketsContent(content);
            content = processString(content);
            if (!content.isEmpty())
                result.add(content );  // 加上花括号后存入列表
        }
        System.out.println(String.join(" ", result));
    }

    public static String processString(String input) {
        StringBuilder result = new StringBuilder();
        boolean skip = false;
        boolean insideBrackets = false;
        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c == '(') {
                insideBrackets = true;
                skip = false;
                result.append(c);
            } else if (c == ')') {
                if (insideBrackets && result.length() > 0 && result.charAt(result.length() - 1) == '(') {
                    skip = true;
                    result.setLength(result.length() - 1);  // Remove the (
                } else {
                    result.append(c);
                }
                insideBrackets = false;
            } else {
                if (!skip) {
                    result.append(c);
                }
            }
        }
        return result.toString().replaceAll("[()]", "");
    }

    // 循环处理括号内的内容
    public static String processBracketsContent(String content) {

        Pattern pattern = Pattern.compile("\\(([^)]+)\\)");
        Matcher matcher = pattern.matcher(content);
        // 使用 StringBuilder 进行逐个处理
        StringBuilder processedContent = new StringBuilder();
        int lastEnd = 0;
        while (matcher.find()) {
            // 添加匹配之前的部分（包括空格）
            int start = matcher.start();
            processedContent.append(content, lastEnd, start);
            int difL = matcher.start() - lastEnd;
            String bracketContent = matcher.group(1);
            // 对括号内的内容进行转换
            String transformedContent = transformContent(bracketContent);
            if (transformedContent.isEmpty() && processedContent.length() >= difL) {
                for (int i = 0; i < difL; i++) {
                    processedContent.deleteCharAt(processedContent.length() - 1);
                }
            }
            processedContent.append("(").append(transformedContent).append(")");
            lastEnd = matcher.end();
        }
        // 添加剩余的部分
        processedContent.append(content.substring(lastEnd));
        return processedContent.toString();
    }

    // 转换括号内的内容
    public static String transformContent(String content) {
        LocalDateTime dateTime = LocalDateTime.now();
        String res = "";
        for (Integer field : list) {
            String dateTimeIdentifier = calendarIdentifierMap.get(field);
            if (content.contains(dateTimeIdentifier) ) {
                if(content.contains("DAY_OF_") && !dateTimeIdentifier.contains("DAY_OF_")) {
                    continue;
                }
                if (content.contains("displayName") ) {
                    res = ConvertMap.map.get(dateTimeIdentifier + ".displayName").convert(content, dateTime, locale, dateTimeIdentifier + ".displayName");
                }else {
                     res = ConvertMap.map.get(dateTimeIdentifier).convert(content, dateTime,locale, dateTimeIdentifier);
                }
                return res;
            }
        }

        return res;
    }
}
